﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ClassesForFtpCrawler
{
    [Serializable]
    public class FTPItem
    {
        public string OriginalString;
        public bool IsDirectory;
        public int Size;
        public DateTime Modified;
        public string Name;
        public string ParentDir;
        public string FullName;
        public string Extension;
        public string ConnectionString;

        public FTPItem() { }

        public FTPItem(ConnectionStringUser FtpConnection, string Path, bool IsDirectory)
        {
            //this function is for relative paths, move to absolute if you have to
            if (Path.StartsWith("ftp:"))
            {
                this.Init(Path, IsDirectory, FtpConnection.Password, FtpConnection.UserName);
            }
            else
            {
                string RelativePath = Path;
                string b = FtpConnection.DataSource;
                if (b.EndsWith("/"))
                {
                    b = b.Remove(b.Length - 1);
                }
                if (RelativePath[0] == '/') { RelativePath = RelativePath.Remove(0, 1); }
                this.ConnectionString = FtpConnection.ToString();
                this.Init(b + "/" + RelativePath, IsDirectory, FtpConnection.Password, FtpConnection.UserName);
            }
        }

        private void Init(string AbsoluteFtpPath, bool IsDirectory, string Password, string UserName)
        {
            this.OriginalString = AbsoluteFtpPath;
            this.IsDirectory = IsDirectory;
            this.Size = 0;
            this.Modified = DateTime.Now;
            this.FullName = AbsoluteFtpPath;
            string[] parts = AbsoluteFtpPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
            this.Name = parts[parts.Length - 1];
            this.ParentDir = string.Empty;
            this.Extension = Path.GetExtension(this.Name);
            for (int i = 0; i < parts.Length - 1; i++)
            {
                if (parts[i].Equals("ftp:"))
                {
                    this.ParentDir += parts[i] + "//";
                }
                else
                {
                    this.ParentDir += parts[i] + "/";
                }
            }
            if (string.IsNullOrEmpty(this.ConnectionString))
            {
                string root = this.OriginalString;
                if (this.OriginalString.IndexOf('/', 7) > 0)
                {
                    root = this.OriginalString.Substring(0, this.OriginalString.IndexOf('/', 7));
                }
                this.ConnectionString = "Data Source=" + this.ParentDir + "; User ID=" + UserName + "; Password=" + Password + ";";
            }

        }

        public FTPItem(string AbsoluteFtpPath, bool IsDirectory, string UserName, string Password)
        {
            this.Init(AbsoluteFtpPath, IsDirectory, Password, UserName);
        }

        public FTPItem(string ItemString, string parent)
        {
            this.ParentDir = parent;
            if (this.ParentDir.EndsWith("/"))
            {
                this.ParentDir = ParentDir.Remove(ParentDir.Length - 1);
            }
            this.OriginalString = ItemString;
            string[] s = ItemString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            this.Size = int.Parse(s[4]);
            this.IsDirectory = OriginalString[0] == 'd';
            this.Name = OriginalString.Substring(54).Trim();
            string m = s[5];
            string d = s[6];
            string y = s[7];
            string t = string.Empty;
            if (y.Contains(":"))
            {
                t = y;
                y = DateTime.Now.Year.ToString();
            }
            Modified = DateTime.Parse(m + " " + d + " " + y + " " + t);
            this.FullName = this.ParentDir + "/" + this.Name;

            if (!this.Name.Contains('.'))
            {
                this.Extension = string.Empty;
            }
            else { this.Extension = this.Name.Substring(this.Name.LastIndexOf('.')); }
        }

    }
}
